package collection.list;

import java.util.Arrays;

public class MyStack<T> {
    private T[] array;
    private int size;
    private int DEFAULT_SIZE = 10;
    private int capacity;

    public MyStack() {
        array = (T[]) new Object[DEFAULT_SIZE];
        size = 0;
        capacity = DEFAULT_SIZE;
    }

    public MyStack(int mySize) {
        array = (T[]) new Object[mySize];
        size = 0;
        capacity = mySize;
    }

    public void checkCapacity() {
        if (size == capacity) {
            T[] newArray = (T[]) new Object[capacity + DEFAULT_SIZE];
            System.arraycopy(array, 0, newArray, 0, size);
            capacity = capacity + DEFAULT_SIZE;
            array = newArray;
        }
    }

    public void push(T element) {
        checkCapacity();
        array[size] = element;
        size++;
    }

    public T peak() {
        return array[size - 1];
    }

    public T pop() {
        T t = array[size - 1];
        array[size - 1] = null;
        size--;
        return t;
    }

    public void show() {
        for (T t : array) {
            if (t != null)
                System.out.print(t + "  ");
        }
        System.out.println();
    }

    @Override
    public String toString() {
        return "MyStack{" +
                "array=" + Arrays.toString(array) +
                '}';
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}
